# _*_ coding:utf-8 _*_
# @File  : user_charts.py
# @Time  : 2021-11-25  11:26
# @Author: zizle

from fastapi import APIRouter, Query
from db import FAConnection
from status import r_status
from configs import WEB_HOST

ucharts_api = APIRouter()


# 获取用户的品种图形
@ucharts_api.get('/', summary='获取品种下的所有图形')
async def get_variety_charts(v: str = Query(..., max_length=2, min_length=1)):
    db = FAConnection(conn_name='admin查询品种图形')
    sql = 'SELECT a.id,a.creator,a.title,a.create_time,a.variety_en,a.sheet_id,a.option_file,' \
          'a.group_name,a.decipherment,a.is_principal,a.is_petit,a.is_private ' \
          'FROM industry_user_chart AS a ' \
          'WHERE a.variety_en=%s;'
    records = db.query(sql, param=[v], keep_conn=True)
    # 查询用户
    user_list = db.query('SELECT id,username FROM user_user;')
    user_dict = {u['id']: u['username'] for u in user_list}

    for item in records:
        item['creator'] = user_dict.get(item['creator'], '未知')
        item['create_time'] = item['create_time'].strftime('%Y-%m-%d')
        item['option_file'] = f'{WEB_HOST}{item["option_file"]}'

    return {'code': r_status.SUCCESS, 'message': '查询品种图形成功!', 'data': records}


